package com.aia.prm.web.ria.app.ui.qne;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import com.vaadin.data.Item;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.TextField;

@SuppressWarnings("serial")
public class QneForm extends Form {
	private static final Logger logger = Logger.getLogger(QneForm.class);
	QneView view = null;

	public QneForm(QneView aview) {
		setIcons();
		view = aview;
		getLayout().setMargin(true);
		setSizeFull();
		
		setFormFieldFactory(new DefaultFieldFactory() {
			public Field createField(Item item, Object propertyId, Component uiContext) {
				Field field = super.createField(item, propertyId, uiContext);
				if (field instanceof TextField) {
					((TextField) field).setNullRepresentation("");
					((TextField) field).setNullSettingAllowed(true);
				}
				if (QneContainer.FIELD_NAME_MAP.containsKey(propertyId)) {
					field.setCaption(QneContainer.FIELD_NAME_MAP.get(propertyId));
				}
				return field;
			}
		});

	}

	private void setIcons() {
		setIcon(new ThemeResource("icons/comment_edit.png"));
	}

	@Override
	public void setItemDataSource(Item newDataSource) {
		if (newDataSource != null) {
			super.setItemDataSource(newDataSource);
			List<Object> orderedProperties = Arrays.asList(QneContainer.NATURAL_COL_ORDER_FORM);
			super.setItemDataSource(newDataSource, orderedProperties);
			setReadOnly(true);
		} else {
			super.setItemDataSource(null);
		}
	}

}
